package com.ruoyi.xiaoshouchuku.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.shangpinxinxi.domain.Shangpinxinxi;
import com.ruoyi.shangpinxinxi.service.IShangpinxinxiService;
import com.ruoyi.xiaoshouchuku.domain.XiaoshouChuku;
import com.ruoyi.xiaoshouchuku.domain.saomaChuku;
import com.ruoyi.xiaoshouchuku.domain.saomaTest;
import com.ruoyi.xiaoshouchuku.service.IXiaoshouChukuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * 销售出库Controller
 *
 * @author ruoyi
 * @date 2023-05-15
 */
@Controller
@RequestMapping("/xiaoshouchuku/SaoMaTest")
public class saomaChukuController extends BaseController {
    private String prefix = "xiaoshouchuku/chukuTest";

    @Autowired
    private IXiaoshouChukuService xiaoshouChukuService;

    @Autowired
    private IShangpinxinxiService shangpinxinxiService;

    @GetMapping("/selectOne")
    public String add() {
        return prefix + "/saoma2showOne";
    }

    @PostMapping("/selectOne/{id}")
    @ResponseBody
    public AjaxResult save(@PathVariable("id") Long id,HttpServletRequest request) {
        XiaoshouChuku xiaoshouChuku = xiaoshouChukuService.selectXiaoshouChukuById(id);
        request.getSession().setAttribute("xiaoshouchuku2saoma",xiaoshouChuku);
        return toAjax(1);
    }

    /**
     * 查询销售出库列表
     */
    @PostMapping("/inserts")
    @ResponseBody
    public AjaxResult inserts(String msg,HttpServletRequest request) {
        msg=msg.replace("\n","");
        Shangpinxinxi shangpinxinxi = shangpinxinxiService.selectShangpinxinxiByDanhao(msg);
        if (shangpinxinxi == null) {
            return toAjax("扫码错误，未找到对应的商品");
        }
        saomaChuku saomaChuku = new saomaChuku();
        Date date = new Date(System.currentTimeMillis());
        saomaChuku.setChukuDate(date);
        XiaoshouChuku xiaoshouChuku = (XiaoshouChuku) request.getSession().getAttribute("xiaoshouchuku2saoma");

        if (shangpinxinxi.getShangpinName() == null) {
            shangpinxinxi.setShangpinName(shangpinxinxi.getShangpinLeibie());
        }
        int shutudo = xiaoshouChukuService.selectTodo2(shangpinxinxi.getShangpinBianhao(),xiaoshouChuku.getChukuDanhao(),xiaoshouChuku.getFahuoDanhao())+1;

        if (shangpinxinxi.getShangpinLeibie().contains("门扇") ||
                shangpinxinxi.getShangpinLeibie().contains("左扇")) {
            if (shutudo > Integer.parseInt(xiaoshouChuku.getMen1Shuliang())) {
                return toAjax("录入的门扇或整套门数量超过订单需求");
            }
        }
        if (shangpinxinxi.getShangpinLeibie().contains("右扇")) {
            if (shutudo > Integer.parseInt(xiaoshouChuku.getMen2Shuliang())) {
                return toAjax("录入的右扇门数量超过订单需求");
            }
        }
        if (shangpinxinxi.getShangpinLeibie().contains("门框")) {
            if (shutudo > Integer.parseInt(xiaoshouChuku.getKuangShuliang())) {
                return toAjax("录入的门框数量超过订单需求");
            }
        }
        if (shangpinxinxi.getShangpinLeibie().contains("防火锁")) {
            if (shutudo > Integer.parseInt(xiaoshouChuku.getShuliang1())) {
                return toAjax("录入的防火锁数量超过订单需求");
            }
        }
        if (shangpinxinxi.getShangpinLeibie().contains("闭门器")) {
            if (shutudo > Integer.parseInt(xiaoshouChuku.getShuliang2())) {
                return toAjax("录入的闭门器数量超过订单需求");
            }
        }
        if (shangpinxinxi.getShangpinLeibie().contains("拉爆盖")) {
            if (shutudo > Integer.parseInt(xiaoshouChuku.getShuliang3())) {
                return toAjax("录入的拉爆盖数量超过订单需求");
            }
        }

        saomaChuku.setChukuName(request.getSession().getAttribute("name4user").toString());
        saomaChuku.setChukuDanhao(xiaoshouChuku.getChukuDanhao());
        saomaChuku.setFahuoDanhao(xiaoshouChuku.getFahuoDanhao());
        saomaChuku.setShangpinBianhao(msg);
        saomaChuku.setShangpinName(shangpinxinxi.getShangpinName());
        saomaChuku.setShangpinShuliang(1);



        HashMap<String,Integer> chuku = new HashMap<>();
        if (xiaoshouChuku.getGoodNameMen1() != null && !xiaoshouChuku.getGoodNameMen1().equals("")) {
            chuku.put(xiaoshouChuku.getGoodNameMen1(),Integer.parseInt(xiaoshouChuku.getMen1Shuliang()));
        }
        else {
            chuku.put("左门扇",-1);
        }
        if (xiaoshouChuku.getGoodNameMen2() != null && !xiaoshouChuku.getGoodNameMen2().equals("")) {
            chuku.put(xiaoshouChuku.getGoodNameMen2(),Integer.parseInt(xiaoshouChuku.getMen2Shuliang()));
        }
        else {
            chuku.put("右门扇",-1);
        }
        if (xiaoshouChuku.getGoodNameKuang() != null && !xiaoshouChuku.getGoodNameKuang().equals("")) {
            chuku.put(xiaoshouChuku.getGoodNameKuang(),Integer.parseInt(xiaoshouChuku.getKuangShuliang()));
        }
        else {
            chuku.put("门框",-1);
        }
        if (xiaoshouChuku.getShuliang1() != null && !xiaoshouChuku.getShuliang1().equals("")) {
            chuku.put("防火锁",Integer.parseInt(xiaoshouChuku.getShuliang1()));
        }
        else {
            chuku.put("防火锁",-1);
        }
        if (xiaoshouChuku.getShuliang2() != null && !xiaoshouChuku.getShuliang2().equals("")) {
            chuku.put("闭门器",Integer.parseInt(xiaoshouChuku.getShuliang2()));
        }
        else {
            chuku.put("闭门器",-1);
        }
        if (xiaoshouChuku.getShuliang3() != null && !xiaoshouChuku.getShuliang3().equals("")) {
            chuku.put("拉爆盖",Integer.parseInt(xiaoshouChuku.getShuliang3()));
        }
        else {
            chuku.put("拉爆盖",-1);
        }

        List<saomaTest> saoma = xiaoshouChukuService.selectXiaoshouChukuByDanhao(xiaoshouChuku.getFahuoDanhao());

        HashMap<String,Integer> saoma2new = new HashMap<>();

        if (saoma != null) {
            for (saomaTest s:saoma) {
                Integer a = Integer.valueOf(s.getShuliang());
                saoma2new.put(s.getName(),a);
            }
            chuku.forEach((key1, value1) -> {
                saoma2new.forEach((key2, value2) -> {
                    if (key1.equals(key2)) {
                        if (Objects.equals(value1, value2)) {
                            saomaTest saomaTest = new saomaTest();
                            saomaTest.setName(key1);
                            saomaTest.setShuliang(String.valueOf(value1));
                            saoma.remove(saomaTest);
                        }
                    }
                });
            });
            if (saoma.size() == 0) {
                xiaoshouChuku.setZhixing("已执行");
                xiaoshouChukuService.updateXiaoshouChuku(xiaoshouChuku);
            }
        }
        xiaoshouChukuService.insertSaomaChuku(saomaChuku);
        return toAjax(1);
    }

    @PostMapping("/selectCheck/{id}")
    @ResponseBody
    public AjaxResult selectCheck(@PathVariable("id") Long id,HttpServletRequest request) {
        XiaoshouChuku xiaoshouChuku = xiaoshouChukuService.selectXiaoshouChukuById(id);
        if (xiaoshouChuku.getZhixing().equals("未执行") || xiaoshouChuku.getZhixing().equals("0")) {
            return toAjax("未执行");
        }else {
            return toAjax("已执行");
        }
    }
}
